﻿using System;
using System.Threading;
using System.Threading.Tasks;
using TiffLibrary;

namespace WholeSlideImageLibrary.Formats.GenericTiff
{
    internal class GenericTiffLevelDecoder
    {
        public GenericTiffLevelDecoder(TiffImageDecoder decoder, double downsample)
        {
            ImageDecoder = decoder;
            Width = decoder.Width;
            Height = decoder.Height;
            Downsample = downsample;
        }

        public TiffImageDecoder ImageDecoder { get; }
        public int Width { get; set; }
        public int Height { get; set; }
        public double Downsample { get; }

        public bool SupportFastJpegReading { get; set; }
        public long[]? Offsets { get; set; }
        public int[]? ByteCounts { get; set; }
        public byte[]? JpegTables { get; set; }

        public Task ReadRegionAsync<T>(int x, int y, int width, int height, Memory<T> buffer, CancellationToken cancellationToken) where T : unmanaged
        {
            var pixelBuffer = new WholeSlideImagePixelBuffer<T>(buffer, width, height);
            return ImageDecoder.DecodeAsync(new TiffPoint(x, y), pixelBuffer, cancellationToken);
        }
    }
}
